package com.fuyao.cloud.admin.flow.service.org.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fuyao.cloud.admin.flow.mapper.ICommentInfoMapper;
import com.fuyao.cloud.admin.flow.model.CommentInfo;
import com.fuyao.cloud.admin.flow.service.org.ICommentInfoService;
import com.fuyao.cloud.common.core.flowable.enums.CommentTypeEnum;
import com.fuyao.cloud.common.security.util.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.flowable.engine.HistoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.runtime.ProcessInstance;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 */

@RequiredArgsConstructor
@Service
public class CommentInfoServiceImpl extends ServiceImpl<ICommentInfoMapper, CommentInfo> implements ICommentInfoService {

    private final ICommentInfoMapper commentInfoMapper;

    private final RuntimeService runtimeService;

    private final HistoryService historyService;

    @Override
    public void saveComment(CommentInfo commentInfo) {
        commentInfo.setTime(new Date());
        if (StrUtil.isBlank(commentInfo.getPersonalCode())) {
            commentInfo.setPersonalCode(SecurityUtils.getUser().getUsername());
        }
        this.saveOrUpdate(commentInfo);
    }

    @Override
    public List<CommentInfo> getCommentInfosByProcessInstanceId(String processInstanceId) {
        List<CommentInfo> commentInfoList = commentInfoMapper.getCommentInfosByProcessInstanceId(processInstanceId);
        if (CollUtil.isNotEmpty(commentInfoList)) {
            commentInfoList.forEach(commentInfo -> {
                if (StrUtil.isNotBlank(commentInfo.getType())) {
                    commentInfo.setTypeName(CommentTypeEnum.getEnumMsgByType(commentInfo.getType()));
                }
            });
        }
        return commentInfoMapper.getCommentInfosByProcessInstanceId(processInstanceId);
    }

    @Override
    public List<CommentInfo> getCommentsByBusinessKey(String businessKey) {
        ProcessInstance processInstance = runtimeService
                .createProcessInstanceQuery()
                .processInstanceBusinessKey(businessKey)
                .singleResult();
        String processInstanceId = "";

        if (processInstance == null) {
            HistoricProcessInstance historicProcessInstance = historyService
                    .createHistoricProcessInstanceQuery()
                    .processInstanceBusinessKey(businessKey)
                    .singleResult();
            if (historicProcessInstance != null) {
                processInstanceId = historicProcessInstance.getId();
            }
        } else {
            processInstanceId = processInstance.getProcessInstanceId();
        }
        if (!StringUtils.hasText(processInstanceId)) {
            return Collections.emptyList();
        }

        return commentInfoMapper.getCommentInfosByProcessInstanceId(processInstanceId);
    }
}
